#include "matrix_key.h"
#include "elog.h"

void matrix_key_init(void) {
}

void matrix_scan()
{
    // scan matrix key
    uint8_t key_num = 0;

    if (HAL_GPIO_ReadPin(ROW1_PORT, ROW1_PIN) == GPIO_PIN_RESET)
    {
        if (HAL_GPIO_ReadPin(COL1_PORT, COL1_PIN) == GPIO_PIN_RESET)
        {
            key_num = 1;
            log_d("Touch key1 pressed");
			return;
        }
        else if (HAL_GPIO_ReadPin(COL2_PORT, COL2_PIN) == GPIO_PIN_RESET)
        {
            key_num = 2;
            log_d("Touch key2 pressed");
            return;
        }
        else if (HAL_GPIO_ReadPin(COL3_PORT, COL3_PIN) == GPIO_PIN_RESET)
        {
            key_num = 3;
            log_d("Touch key3 pressed");
            return;
        }
    }
    
    if (HAL_GPIO_ReadPin(ROW2_PORT, ROW2_PIN) == GPIO_PIN_RESET)
    {
        if (HAL_GPIO_ReadPin(COL1_PORT, COL1_PIN) == GPIO_PIN_RESET)
        {
            key_num = 4;
            log_d("Touch key4 pressed");
            return;
        }
        else if (HAL_GPIO_ReadPin(COL2_PORT, COL2_PIN) == GPIO_PIN_RESET)
        {
            key_num = 5;
            log_d("Touch key5 pressed");
            return;
        }
        else if (HAL_GPIO_ReadPin(COL3_PORT, COL3_PIN) == GPIO_PIN_RESET)
        {
            key_num = 6;
            log_d("Touch key6 pressed");
            return;
        }
    }
    
    if (HAL_GPIO_ReadPin(ROW3_PORT, ROW3_PIN) == GPIO_PIN_RESET)
    {
        if (HAL_GPIO_ReadPin(COL1_PORT, COL1_PIN) == GPIO_PIN_RESET)
        {
            key_num = 7;
            log_d("Touch key7 pressed");
            return;
        }
        else if (HAL_GPIO_ReadPin(COL2_PORT, COL2_PIN) == GPIO_PIN_RESET)
        {
            key_num = 8;
            log_d("Touch key8 pressed");
            return;
        }
        else if (HAL_GPIO_ReadPin(COL3_PORT, COL3_PIN) == GPIO_PIN_RESET)
        {
            key_num = 9;
            log_d("Touch key9 pressed");
            return;
        }
    }
    
    if (HAL_GPIO_ReadPin(ROW4_PORT, ROW4_PIN) == GPIO_PIN_RESET)
    {
        if (HAL_GPIO_ReadPin(COL1_PORT, COL1_PIN) == GPIO_PIN_RESET)
        {
            key_num = 10;
            log_d("Touch key10 pressed");
            return;
        }
        else if (HAL_GPIO_ReadPin(COL2_PORT, COL2_PIN) == GPIO_PIN_RESET)
        {
            key_num = 11;
            log_d("Touch key11 pressed");
            return;
        }
        else if (HAL_GPIO_ReadPin(COL3_PORT, COL3_PIN) == GPIO_PIN_RESET)
        {
            key_num = 12;
            log_d("Touch key12 pressed");
            return;
        }
    }
}